package com.situ.stmall.manger.mapper;

import com.situ.stmall.manger.entity.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper  //MyBatis的注解，
public interface CategoryMapper {
    /*添加
    返回值类型：int
    方法名：insert
    参数：Category
    配置:@insert
    */
    @Insert({"insert into category ",
            "(name,dscp,pic,parent_id,recom)",
            "value (#{name },#{dscp},#{pic},#{parentId},#{recom})",
    })
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insert(Category category);

    /*
     * 删除 根据Id删除
     */
    @Delete("delete from category where id =#{id}")
    int delete(Integer id);


    /*动态sql更新,需要为SQL添加<script></script>*/
    @Update({"<script>",
            "update category",
            "<set>",
            "<if test='name!=null and name.length>0'> name=#{name},</if>",
            "<if test='dscp!=null and dscp.length>0'> dscp=#{dscp},</if>",
            "<if test='pic!=null and pic.length>0'> pic=#{pic},</if>",
            "<if test='parentId!=null'> parent_id=#{parentId},</if>",
            "<if test='status!=null'> status=#{status},</if>",
            "<if test='recom!=null'> recom=#{recom}</if>",
            "</set>",
            "where id =#{id}",
            "</script>"
    })
    int update(Category category);

    /*查询*/
    //根据ID查询
    @Select("select * from category where id=#{id}")
    Category selectById(Integer id);

    //条件查询
    @Select({"<script>",
            "select * from category",
            "<where>",
            "<if test='name!= null and name.length>0'> name like'%${name}%'</if>",
            "<if test='parentId!= null and parentId!=0'>and parent_id=#{parentId} </if>",
            "<if test='parentId!= null and parentId==0'>and parent_id is null</if>",
            "<if test='recom!= null'>and recom=#{recom} </if>",
            "<if test='status!= null'>and status=#{status} </if>",
            "</where>",
            "</script>"
    })
    @Results({  //相当于<resultMap>的标签
            @Result(column = "parent_id", property = "parentId"),
            @Result(column = "parent_id", property = "parent",
                    one = @One(select = "selectById"))//相当<id><result>标签

    })
    List<Category> select(Category category);

}
